package com.ruoyi.system.service.impl;

import java.util.List;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Bed;
import com.ruoyi.system.domain.CheckInLeave;
import com.ruoyi.system.domain.CheckInLog;
import com.ruoyi.system.mapper.BedMapper;
import com.ruoyi.system.mapper.CheckInLeaveMapper;
import com.ruoyi.system.mapper.CheckInLogMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ElderlyMapper;
import com.ruoyi.system.domain.Elderly;
import com.ruoyi.system.service.IElderlyService;

/**
 * 老年信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@Service
public class ElderlyServiceImpl implements IElderlyService
{
    @Autowired
    private ElderlyMapper elderlyMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private CheckInLogMapper checkInLogMapper;

    /**
     * 查询老年信息
     *
     * @param id 老年信息主键
     * @return 老年信息
     */
    @Override
    public Elderly selectElderlyById(Long id)
    {
        return elderlyMapper.selectElderlyById(id);
    }

    /**
     * 查询老年信息列表
     *
     * @param elderly 老年信息
     * @return 老年信息
     */
    @Override
    public List<Elderly> selectElderlyList(Elderly elderly)
    {
        return elderlyMapper.selectElderlyList(elderly);
    }

    /**
     * 新增老年信息
     *
     * @param elderly 老年信息
     * @return 结果
     */
    @Override
    public int insertElderly(Elderly elderly)
    {
        elderly.setCreateTime(DateUtils.getNowDate());
        return elderlyMapper.insertElderly(elderly);
    }

    /**
     * 修改老年信息
     *
     * @param elderly 老年信息
     * @return 结果
     */
    @Override
    public int updateElderly(Elderly elderly)
    {
        elderly.setUpdateTime(DateUtils.getNowDate());
        return elderlyMapper.updateElderly(elderly);
    }
    /**
     * 退住入住
     *
     * @param elderly 老年信息
     * @return 结果
     */
    @Override
    public int check(Elderly elderly) {
        if (elderly.getCheckInStatus().equals("0")) {
            //入住
            elderly.setOutTime(null);
            //床位信息更新
            Bed bed = bedMapper.selectBedById(elderly.getBedId());
            bed.setPersonId(elderly.getId());
            bed.setPersonName(elderly.getName());
            bed.setBedStatus(String.valueOf(1));//0-空闲；1-已入住
            bedMapper.updateBed(bed);
            //新增一条入住记录
            CheckInLog checkInLog = new CheckInLog();
            checkInLog.setPersonId(elderly.getId());
            checkInLog.setPersonName(elderly.getName());
            checkInLog.setLogType(String.valueOf(0));//0-入住；1-退住
            checkInLog.setCheckInTime(elderly.getCheckInTime());
            checkInLogMapper.insertCheckInLog(checkInLog);
        } else if (elderly.getCheckInStatus().equals("1")) {
            //退住
            elderly.setCheckInTime(null);
            //床位信息更新
            Bed bed = bedMapper.selectBedById(elderly.getBedId());
            bed.setPersonId(null);
            bed.setPersonName(null);
            bed.setBedStatus(String.valueOf(0));//0-空闲；1-已入住
            bedMapper.updateBed(bed);
            //新增一条入住记录
            CheckInLog checkInLog = new CheckInLog();
            checkInLog.setPersonId(elderly.getId());
            checkInLog.setPersonName(elderly.getName());
            checkInLog.setLogType(String.valueOf(1));//0-入住；1-退住
            checkInLog.setOutTime(elderly.getOutTime());
            checkInLogMapper.insertCheckInLog(checkInLog);
        }
        elderly.setUpdateTime(DateUtils.getNowDate());
        return elderlyMapper.updateElderly(elderly);
    }

    /**
     * 批量删除老年信息
     *
     * @param ids 需要删除的老年信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyByIds(Long[] ids)
    {
        return elderlyMapper.deleteElderlyByIds(ids);
    }

    /**
     * 删除老年信息信息
     *
     * @param id 老年信息主键
     * @return 结果
     */
    @Override
    public int deleteElderlyById(Long id)
    {
        return elderlyMapper.deleteElderlyById(id);
    }

    @Override
    public
    List<Elderly> selectElderlyAll(){
        return elderlyMapper.selectElderlyAll();
    }
}
